#!/usr/bin/python
# Splits a fastq file into multiple.
import sys

# Parameters.
num_files = int(sys.argv[1])
input_file = sys.argv[2]
output_base = sys.argv[3]
output_extn = sys.argv[4]

# Open all output files.
outputs = []
for i in range(num_files):
	name = "%s_%i.%s" % (output_base, i, output_extn)
	outputs.append(open(name, "wb"))
	
# Stream input splitting on the fly.
i = -1
fin = open(input_file, "rb")
for line in fin:
	# check if we start new entry.
	if line[0] == ">":
		# Increment index.
		if i == num_files - 1:
			i = 0
		else:
			i += 1
		
	# write line to new output.
	outputs[i].write(line)

# Close all output files.
for output in outputs:
	output.close()
